package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.LoginBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.view.DataLoadingDialog;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import es.dmoral.toasty.Toasty;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/11/2.
 */
@ContentView(R.layout.fragment_passwordlogin)
public class PasswordLogin extends BaseFragment {

    @ViewInject(R.id.et_passwordlogin_account)
    private EditText etAccount;
    @ViewInject(R.id.et_passwordlogin_password)
    private EditText etPwd;
    @ViewInject(R.id.bt_passwordlogin_login)
    private Button btLogin;
    public static final String LOGIN_STATUS = "com.genesis.yunnanji.login";
    private DataLoadingDialog dialog;

    public static PasswordLogin newInstance(String param) {
        PasswordLogin passwordLogin = new PasswordLogin();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        passwordLogin.setArguments(bundle);
        return passwordLogin;
    }

    @Override
    protected void initView() {
        dialog = new DataLoadingDialog(getContext());
    }

    @Override
    protected void setUpView() {

    }

    @Event(value = {R.id.bt_passwordlogin_login}, type = View.OnClickListener.class)
    private void click(View v) {
        switch (v.getId()) {
            case R.id.bt_passwordlogin_login:
                if (!TextUtils.isEmpty(etAccount.getText().toString()) && !TextUtils.isEmpty(etPwd.getText().toString())) {
                    dialog.show();
                    Login();
                } else {
                    Toast.makeText(context, "请填写账号密码", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public void Login() {
        OkHttpUtils
                .post()
                .url(GenesisApiConfig.HOST + GenesisApiConfig.LOGIN)
                .addParams("uname", etAccount.getText().toString())
                .addParams("upwd", genesisUtils.getMD5Str("jzwhcb" + genesisUtils.getMD5Str(etPwd.getText().toString())))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        dialog.dismiss();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        dialog.dismiss();
                        Gson gson = new Gson();
                        LoginBean loginBean = gson.fromJson(response, LoginBean.class);
                        if (loginBean.getStatus() == 200) {
                            genesisUtils.setAppToken(loginBean.getResult().get__apptoken__());
                            Intent intent = new Intent();
                            intent.setAction(LOGIN_STATUS);
                            intent.putExtra("msg", "login");
                            EventBus.getDefault().post(new String("login"));
                            PasswordLogin.this.getActivity().sendBroadcast(intent);
                            PasswordLogin.this.getActivity().finish();
                            Toasty.success(context, loginBean.getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toasty.error(context, loginBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }

}
